//
//  HeadView.m
//  WYNews
//
//  Created by lanou3g on 15/6/2.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "HeadView.h"
#define WIDTH [UIScreen mainScreen].bounds.size.width
//#define SELF_WIDETH [UIScreen mainScreen].bounds.size.width

@interface HeadView()
{
    NSMutableArray * _btArray;
    UIImageView * imgView4;
    UILabel * titleLable4;
    UIImageView * imgView;
    UILabel * titleLable;
    UIImageView * imgView1;
    UILabel * titleLable1;
    UIImageView * imgView2;
    UILabel * titleLable2;
    UIImageView * imgView3;
    UILabel * titleLable3;
}

@end

@implementation HeadView

-(instancetype)initWithFrame:(CGRect)frame{
    self=[super initWithFrame:frame];
    if (self) {
        self.frame=frame;
        
        self.ModelArray=[[NSMutableArray alloc]init];
        [self setupViewsWith:frame];
        
        NSArray * dataArray = [DataBaseHandle getDataArrayWithTitleid:NSStringFromClass([self class])];
        if (dataArray) {
            self.ModelArray = [dataArray mutableCopy];
            [self refreshViewUI];
        }else{
            [self refreshHeadViw];
        }
    }
    return self;
}

-(void)refreshHeadViw
{
    //网络请求数据
    NSString * urlString=@"http://c.m.163.com/nc/ad/headline/0-4.html";
    NSURL * url=[[NSURL alloc]initWithString:urlString];
    //创建请求对象
    NSURLRequest * request=[NSURLRequest requestWithURL:url cachePolicy:(NSURLRequestReloadIgnoringLocalCacheData) timeoutInterval:60.0];
    //连接方式对象
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSDictionary * dic=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        DataModel * model4= [DataBaseHandle getDataArrayWithTitleid:HeadViewKey].lastObject;
        [self.ModelArray addObject:model4];
        NSArray * array=dic[@"headline_ad"];
        for (int i=0; i<array.count; i++) {
            HeadModel * model=[[HeadModel alloc]init];
            NSDictionary * dic=array[i];
            [model setValuesForKeysWithDictionary:dic];
            [self.ModelArray addObject:model];
        }
        
        [DataBaseHandle insertDBWWithArra:_ModelArray byID:NSStringFromClass([self class])];
        
        [self refreshViewUI];
    }];
}

-(void)setupViewsWith:(CGRect)frame
{
    //创建scrollView
    self.scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
    self.scrollView.contentSize=CGSizeMake(5*frame.size.width, frame.size.height);
    self.scrollView.pagingEnabled=YES;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    
    //创建imgview
    imgView4=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height-0.053*WIDTH-6)];
    imgView4.contentMode = UIViewContentModeScaleAspectFill;
    imgView4.clipsToBounds = YES;
    
    //创建lable
    titleLable4=[[UILabel alloc]initWithFrame:CGRectMake(0,self.scrollView.frame.size.height-0.053*WIDTH -3, self.scrollView.frame.size.width, 0.053*WIDTH)];
    titleLable4.font = [UIFont fontWithName:WAWA_FONT size:TitleFont_Size-2];
    
    UIButton * button4=[UIButton buttonWithType:(UIButtonTypeSystem)];
    button4.backgroundColor=[UIColor clearColor];
    button4.frame=CGRectMake(0, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height);
    [button4 addTarget:self action:@selector(pushPhotoViewController:) forControlEvents:(UIControlEventTouchUpInside )];
    
    
    
    //创建imgview 添加到scrollview上面
    imgView=[[UIImageView alloc]initWithFrame:CGRectMake(self.scrollView.frame.size.width, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height-0.053*WIDTH-6)];
    imgView.contentMode = UIViewContentModeScaleAspectFill;
    imgView.clipsToBounds = YES;
    
    titleLable=[[UILabel alloc]initWithFrame:CGRectMake(self.scrollView.frame.size.width,self.scrollView.frame.size.height-0.053*WIDTH-3 , self.scrollView.frame.size.width, 0.053*WIDTH )];
    titleLable.font = [UIFont fontWithName:WAWA_FONT size:TitleFont_Size-2];
    
    
    UIButton * button=[UIButton buttonWithType:(UIButtonTypeSystem)];
    button.backgroundColor=[UIColor clearColor];
    button.frame=CGRectMake(self.scrollView.frame.size.width, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height);
    [button addTarget:self action:@selector(pushPhotoViewController:) forControlEvents:(UIControlEventTouchUpInside )];
    
    
    imgView1=[[UIImageView alloc]initWithFrame:CGRectMake(2*self.scrollView.frame.size.width, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height-0.053*WIDTH-6)];
    imgView1.contentMode = UIViewContentModeScaleAspectFill;
    imgView1.clipsToBounds = YES;
    
    titleLable1=[[UILabel alloc]initWithFrame:CGRectMake(2*self.scrollView.frame.size.width,self.scrollView.frame.size.height-0.053*WIDTH -3, self.scrollView.frame.size.width, 0.053*WIDTH)];
    titleLable1.font = [UIFont fontWithName:WAWA_FONT size:TitleFont_Size-2];
    
    UIButton * button1=[UIButton buttonWithType:(UIButtonTypeSystem)];
    button1.backgroundColor=[UIColor clearColor];
    button1.frame=CGRectMake(2*self.scrollView.frame.size.width, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height);
    [button1 addTarget:self action:@selector(pushPhotoViewController:) forControlEvents:(UIControlEventTouchUpInside )];
    
    
    
    imgView2=[[UIImageView alloc]initWithFrame:CGRectMake(3*self.scrollView.frame.size.width, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height-0.053*WIDTH-6)];
    imgView2.contentMode = UIViewContentModeScaleAspectFill;
    imgView2.clipsToBounds = YES;
    
    titleLable2=[[UILabel alloc]initWithFrame:CGRectMake(3*self.scrollView.frame.size.width,self.scrollView.frame.size.height-0.053*WIDTH-3 , self.scrollView.frame.size.width, 0.053*WIDTH)];
    titleLable2.font = [UIFont fontWithName:WAWA_FONT size:TitleFont_Size-2];
    
    UIButton * button2=[UIButton buttonWithType:(UIButtonTypeSystem)];
    button2.backgroundColor=[UIColor clearColor];
    button2.frame=CGRectMake(3*self.scrollView.frame.size.width, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height);
    [button2 addTarget:self action:@selector(pushPhotoViewController:) forControlEvents:(UIControlEventTouchUpInside )];
    
    
    
    
    imgView3=[[UIImageView alloc]initWithFrame:CGRectMake(4*self.scrollView.frame.size.width, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height-0.053*WIDTH-5)];
    imgView3.contentMode = UIViewContentModeScaleAspectFill;
    imgView3.clipsToBounds = YES;
    
    titleLable3=[[UILabel alloc]initWithFrame:CGRectMake(4*self.scrollView.frame.size.width,self.scrollView.frame.size.height-0.053*WIDTH -3, self.scrollView.frame.size.width, 0.053*WIDTH)];
    titleLable3.font = [UIFont fontWithName:WAWA_FONT size:TitleFont_Size-2];
    
    UIButton * button3=[UIButton buttonWithType:(UIButtonTypeSystem)];
    button3.backgroundColor=[UIColor clearColor];
    button3.frame=CGRectMake(4*self.scrollView.frame.size.width, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height);
    [button3 addTarget:self action:@selector(pushPhotoViewController:) forControlEvents:(UIControlEventTouchUpInside )];
    
    
    _btArray =[[NSMutableArray alloc]init];
    [_btArray addObject:button4];
    [_btArray addObject:button];
    [_btArray addObject:button1];
    [_btArray addObject:button2];
    [_btArray addObject:button3];
    
    [self.scrollView addSubview:imgView];
    [self.scrollView addSubview:imgView1];
    [self.scrollView addSubview:imgView2];
    [self.scrollView addSubview:imgView3];
    [self.scrollView addSubview:imgView4];
    [self.scrollView addSubview:titleLable];
    [self.scrollView addSubview:titleLable1];
    [self.scrollView addSubview:titleLable2];
    [self.scrollView addSubview:titleLable3];
    [self.scrollView addSubview:titleLable4];
    [self.scrollView addSubview:button];
    [self.scrollView addSubview:button1];
    [self.scrollView addSubview:button2];
    [self.scrollView addSubview:button3];
    [self.scrollView addSubview:button4];
    
    [self addSubview:self.scrollView];

}

-(void)refreshViewUI
{
    DataModel * model4= self.ModelArray[0];
    [imgView4 sd_setImageWithURL:[NSURL URLWithString:model4.imgsrc]];
    titleLable4.text=model4.title;
    
    
    HeadModel * model=self.ModelArray[1];
    [imgView sd_setImageWithURL:[NSURL URLWithString:model.imgsrc]];
    titleLable.text=model.title;
    
    HeadModel * model1=self.ModelArray[2];
    [imgView1 sd_setImageWithURL:[NSURL URLWithString:model1.imgsrc]];
    titleLable1.text=model1.title;

    
    HeadModel * model2=self.ModelArray[3];
    [imgView2 sd_setImageWithURL:[NSURL URLWithString:model2.imgsrc]];
    titleLable2.text=model2.title;
    
    HeadModel * model3=self.ModelArray[4];
    [imgView3 sd_setImageWithURL:[NSURL URLWithString:model3.imgsrc]];
    titleLable3.text=model3.title;

}
-(void)pushPhotoViewController:(UIButton *)BI{
    
    NSInteger index = [_btArray indexOfObject:BI];
    if (index==0) {
        DataModel * model=_ModelArray[index];
        NSString * str=model.photosetID;
        
        if (!str) {
            str = model.docid;
        }
        
        [self.delegate pushphotoDetailViewControllerWithID:str];
        
    }else{
        HeadModel * model=_ModelArray[index];
        NSString * str=[[model.url stringByReplacingOccurrencesOfString:@"|" withString:@"/"] substringFromIndex:4];
        
        [self.delegate pushphotoDetailViewControllerWithID:str];
    }
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
